package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.manor.ManorHarvestModel;
import com.yanqu.road.server.gameplayer.module.manor.ManorModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.manor.ManorMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * @author ：zfr
 * @date ：2021/6/8 14:16
 */
public class UseAddManorOutPut extends BaseUseGoods {

    public UseAddManorOutPut(GamePlayer player, GoodsInfo goodsInfo, long param) {
        super(player, goodsInfo, param);
    }

    @Override
    int checkUse(long count) {
        if (player.getModule(ManorModule.class).getUserManorData() == null) {
            return GameErrorCode.E_BAG_MANOR_SYSTEM_LOCK;
        }
        ManorHarvestModel harvestModel = player.getModule(ManorModule.class).getHarvestModel();
        if (!harvestModel.isUnLockClassification(goodsInfo.getParamList().get(1).intValue())) {
            return GameErrorCode.E_BAG_MANOR_HARVEST_CLASSIFICATION_LOCK;
        }
        return 0;
    }

    @Override
    UseGoodsResult executeUse(long count) {
        UseGoodsResult result = new UseGoodsResult();
        int timeMin = goodsInfo.getParamList().get(0).intValue();
        int classification = goodsInfo.getParamList().get(1).intValue();
        Property useGoodGet = player.getModule(ManorModule.class).getHarvestModel().getUseGoodGet(timeMin, classification, count);
        player.getModule(CurrencyModule.class).addCurrency(useGoodGet, eLogMoneyType.Manor, eLogMoneyType.ManorUseGoodGet);
        result.setReward(PropertyHelper.parsePropertyToString(useGoodGet));
        long yieldCount = 0L;
        for (BigInteger value : useGoodGet.getGoods().values()) {
            yieldCount += value.longValue();
        }
        ManorMgr.addRankActivityYieldCount(player.getModule(ManorModule.class).getUserManorData(), yieldCount, player);
        return result;
    }

}
